package cn.com.dcsgo.controller;

import cn.com.dcsgo.common.Result;
import cn.com.dcsgo.domain.dto.SysDeptSaveDTO;
import cn.com.dcsgo.domain.dto.SysDeptUpdateDTO;
import cn.com.dcsgo.domain.po.SysDept;
import cn.com.dcsgo.domain.vo.SysDeptVO;
import cn.com.dcsgo.permission.MyPermission;
import cn.com.dcsgo.service.SysDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * SysDeptController
 *
 * @author Dcsgo
 * @since 2/6/2025 下午 8:01
 */
@Tag(name = "部门")
@RestController
@RequestMapping("sysDept")
@RequiredArgsConstructor
public class SysDeptController extends BaseController {
    private final SysDeptService sysDeptService;

    @MyPermission({"system:dept:query"})
    @Operation(summary = "查询所有部门", description = "返回具有层级关系的部门信息")
    @GetMapping("listDept")
    public Result<List<SysDeptVO>> getList() {
        List<SysDeptVO> sysDeptVOList = sysDeptService.getLevelList();
        return new Result<>(sysDeptVOList);
    }

    @MyPermission({"system:dept:query"})
    @Operation(summary = "根据id获取部门信息")
    @GetMapping("get/{deptId}")
    public Result<SysDept> getOne(@PathVariable Long deptId) {
        return new Result<>(sysDeptService.getOne(deptId));
    }

    @MyPermission({"system:dept:add"})
    @Operation(summary = "添加部门")
    @PostMapping("save")
    public Result<Serializable> save(@Validated @RequestBody SysDeptSaveDTO saveDTO) {
        int row = sysDeptService.save(saveDTO);
        return chooseReturn(row > 0);
    }

    @MyPermission({"system:dept:edit"})
    @Operation(summary = "修改部门")
    @PutMapping("update")
    public Result<Serializable> update(@Validated @RequestBody SysDeptUpdateDTO updateDTO) {
        int row = sysDeptService.update(updateDTO);
        return chooseReturn(row > 0);
    }

    @MyPermission({"system:dept:remove"})
    @Operation(summary = "删除部门", description = "有下级部门的部门不允许直接删除")
    @DeleteMapping("delete/{deptId}")
    public Result<Serializable> delete(@PathVariable Long deptId) {
        int row = sysDeptService.remove(deptId);
        return chooseReturn(row > 0);
    }
}
